﻿using System;
using System.Text.RegularExpressions;

namespace RomanNumeralKata.Core.Utility
{
    /// <summary>
    ///     validation using a regex rather than brute force, or random checks
    /// </summary>
    public class RomanNumeralRegExValidator : IRomanNumeralValidator
    {
        /// <summary>
        ///     Makes sure roman numeral string only has valid roman 'digits'
        /// </summary>
        private static readonly Regex _validRomanNumeralExpression =
            new Regex("^M{0,3}(CM|CD|D?C{0,3})(XC|XL|L?X{0,3})(IX|IV|V?I{0,3})$",
                RegexOptions.IgnoreCase | RegexOptions.Compiled);

        public bool IsValidRomanNumeral(string romanNumeralToTest)
        {
            if (null == romanNumeralToTest)
                throw new ArgumentNullException("romanNumeralToTest",
                    "The string passed in was null.  If you mean to represent an Arabic zero (0), pass a blank string");

            //short circuit, we could handle nulla or N for zero?
            return romanNumeralToTest == String.Empty
                   || _validRomanNumeralExpression.IsMatch(romanNumeralToTest);
        }

        public bool IsValidRomanNumeral(char romanNumeralToTest)
        {
            return IsValidRomanNumeral(romanNumeralToTest.ToString());
        }
    }
}